//一般直接写在一个js文件中
layui.use(['element','form','laydate','upload','layer'],function () {
    var $ = layui.jquery
        ,layer = layui.layer
        ,upload = layui.upload;
    upload.render({
        elem: '#img1'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            var that = this;
            obj.preview(function(index, file, result){
                $(that.elem).attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            //上传成功
            var $target = $(this.elem);
            $target.parent().find(".hiddenIpt").val(res.data.path);
        }
        ,error: function(){
            //演示失败状态
            var $target = $(this.elem);
            var demoText = $target.parent().find(".msg-box")
            demoText.html('<span style="color: #FF5722;">上传失败!</span>');
        }
    });
});